package com.swjd.sell.contorller;

import com.swjd.sell.pojo.ProductCategory;
import com.swjd.sell.pojo.ProductInfo;
import com.swjd.sell.service.ProductCategoryService;
import com.swjd.sell.service.ProductInfoService;
import com.swjd.sell.utils.ResultVOUtil;
import com.swjd.sell.vo.ProductInfoVO;
import com.swjd.sell.vo.ProductVO;
import com.swjd.sell.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 买家商品
 *
 * @author 刘涛
 * @create 2021/8/25 14:39
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 商品展示
     */
    @GetMapping("/list")
    public ResultVO list() {
        ArrayList<Integer> productTypeList = new ArrayList<>();
        ArrayList<ProductVO> productVOList = new ArrayList<>();
        //1.查询所有上架的商品
        List<ProductInfo> upAll = productInfoService.selectUpAll();
        //2.查询类目（一次性查询）
        for (ProductInfo productInfo : upAll) {
            productTypeList.add(productInfo.getCategoryType());
        }
        List<ProductCategory> categories = productCategoryService.selectByCategoryTypeIn(productTypeList);
        //3.数据拼装
        //填充productVO数据
        for (ProductCategory category : categories) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(category.getCategoryName());
            productVO.setCategoryType(category.getCategoryType());
            ArrayList<ProductInfoVO> infoVOList = new ArrayList<>();
            //填充productInfoVO数据
            for (ProductInfo productInfo : upAll) {
                //商品信息类目编号跟商品类目编号相同才设置
                if (category.getCategoryType().equals(productInfo.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    productInfoVO.setProductId(productInfo.getProductId());
                    productInfoVO.setProductIcon(productInfo.getProductIcon());
                    productInfoVO.setProductPrice(productInfo.getProductPrice());
                    productInfoVO.setProductName(productInfo.getProductName());
                    productInfoVO.setProductDescription(productInfo.getProductDescription());
                    //数据存到infoVOArrayList
                    infoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(infoVOList);
            //拼装productVO和productInfoVO
            productVOList.add(productVO);
        }

        return ResultVOUtil.success(productVOList);
    }
}
